package org.grottarossa.ui.core.container;


import java.io.Serializable;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.Hashtable;

import org.grottarossa.template.data.enums.EnumTaskType;
import org.grottarossa.template.data.pojo.tasks.Task;
import org.grottarossa.template.data.pojo.tasks.TaskList;
import org.grottarossa.template.drivers.components.tokens.CaseManagementId;
import org.grottarossa.template.drivers.components.tokens.TaskManagementId;
import org.grottarossa.template.drivers.components.tokens.WorkFlowId;
import org.grottarossa.ui.DateHelper;
import org.grottarossa.ui.core.BPMResource;

import com.vaadin.data.util.HierarchicalContainer;
import com.vaadin.terminal.Sizeable;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.AbstractSelect.NewItemHandler;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.Runo;

public class TaskContainer extends HierarchicalContainer implements Serializable, ClickListener
{
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;


	public static String ICON = "   ";
	public static String SKIP = "Skip";
	public static String COMPITO ="Compito";
	public static String STATO ="Stato";
	public static String STATO_VAL ="StatoVal";
	public static String COMPLETATO ="Completato";
	public static String ASSEGNATO_A ="Assegnato a";
	public static String SCADENZA ="Scadenza";
	public static String TYPE ="Type";
	public static String WORKFLOW_ID ="WorkflowID";
	public static String WORKFLOW_VERSION ="WorkflowVersion";
	public static String PROCESS_REPOSITORY_FIELD ="ProcessRepositoryField";
	public static String PROCESS_SCRIPT_NAME ="ProcessScriptName";
	public static String TASK_MANAGMENT_ID ="TaskId";
	public static String CASE_MANAGEMENT_ID ="CaseFolderId";
	public static String SCRIPT ="Script";

	public static String[] VISIBLE_COLUMNS = { ICON, "Skip", "Compito","Stato", "Completato","Assegnato a", "Scadenza"};
	public static String[] HEADER_COLUMNS = {ICON, "Skip", "Compito","Stato", "%","Assegnato a", "Scadenza"};

	public static String NOP="Not a process";

	private Hashtable<Button, Task> m_skipButtonsMap = new Hashtable<Button, Task>();
	private Hashtable<Button, Task> m_assignButtonsMap = new Hashtable<Button, Task>();
	private Hashtable<Button, Task> m_expireButtonsMap = new Hashtable<Button, Task>();

	private final Window m_currentWindow;
	private ArrayList<ITaskPanelSkipListener> m_skipListeners = new ArrayList<ITaskPanelSkipListener>();
	private ArrayList<ITaskPanelAssignListener> m_assignListeners = new ArrayList<ITaskPanelAssignListener>() ;
	private ArrayList<ITaskPanelExpireListener> m_expireListeners = new ArrayList<ITaskPanelExpireListener>() ;

	public TaskContainer(TaskList taskList, Window currentWindow)
	{
		super();
		this.m_currentWindow = currentWindow;
		addContainerProperty(ICON, String.class, null);
		addContainerProperty(SKIP, Button.class, null);
		addContainerProperty(COMPITO, String.class, null);
		addContainerProperty(STATO, String.class, null);
		addContainerProperty(STATO_VAL, Integer.class, null);
		addContainerProperty(COMPLETATO, Integer.class, null);
		addContainerProperty(ASSEGNATO_A, Button.class, null);
		addContainerProperty(SCADENZA, Button.class, null);
		addContainerProperty(TYPE, EnumTaskType.class, null);

		addContainerProperty(WORKFLOW_ID, WorkFlowId.class, null);
		addContainerProperty(WORKFLOW_VERSION, Integer.class, null);
		addContainerProperty(PROCESS_REPOSITORY_FIELD, String.class, null);
		addContainerProperty(PROCESS_SCRIPT_NAME, String.class, null);

		addContainerProperty(TASK_MANAGMENT_ID, TaskManagementId.class, null);
		addContainerProperty(CASE_MANAGEMENT_ID, CaseManagementId.class, null);
		addContainerProperty(SCRIPT, String.class, null);

		Object parent = null;

		for( Task task : taskList)
		{
			Object obj = addItem();
			getContainerProperty(obj, "Compito").setValue(task.getTaskName());

			getContainerProperty(obj, ICON).setValue("");

			if ( task.isSection() )
			{
				parent = obj;
			}
			else if ( parent != null)
			{
				Button btnSkip = new Button("Skip");
				btnSkip.addListener(this);
				btnSkip.setStyleName("button-transparent");
				m_skipButtonsMap.put(btnSkip, task);

				com.vaadin.ui.
				Button btnAssign = new Button("...");
				btnAssign.setStyleName("button-transparent");
				btnAssign.addListener(this);
				btnAssign.setWidth(98, Sizeable.UNITS_PERCENTAGE);
				btnAssign.setIcon(BPMResource.ICON_TASK_ASSIGN);
				m_assignButtonsMap.put(btnAssign, task);

				Button btnExpire = new Button("...");

				btnExpire.setStyleName("button-transparent");
				btnExpire.addListener(this);
				btnExpire.setWidth(98, Sizeable.UNITS_PERCENTAGE);
				btnExpire.setIcon(BPMResource.ICON_TASK_EXPIRE);
				m_expireButtonsMap.put(btnExpire, task);

				setParent(obj, parent);
				getContainerProperty(obj, SKIP).setValue(btnSkip);
				getContainerProperty(obj, STATO).setValue(task.getStatus().name());
				getContainerProperty(obj, STATO_VAL).setValue(task.getStatus().ordinal());
				getContainerProperty(obj, COMPLETATO).setValue(task.getPercentCompleted());
				getContainerProperty(obj, SCRIPT).setValue(task.getScriptName());
				getContainerProperty(obj, TASK_MANAGMENT_ID).setValue(task.getTaskID());
				getContainerProperty(obj, CASE_MANAGEMENT_ID).setValue(task.getTaskContainerID());

				if ( (task.getAssignee()!=null) && (task.getAssignee().getDisplayName() != null ))
					btnAssign.setCaption(task.getAssignee().getDisplayName());

				getContainerProperty(obj, ASSEGNATO_A).setValue(btnAssign);

				if ( (task.getDeadLine()!=null) && (task.getDeadLine().getDeadLineDate() != null ))
					btnExpire.setCaption(DateHelper.DateToString(task.getDeadLine().getTriggerDate()));

				getContainerProperty(obj, SCADENZA).setValue(btnExpire);

				this.setChildrenAllowed(obj,false);
			}

			if ( task.isProcess() )
			{
				getContainerProperty(obj, WORKFLOW_ID).setValue(task.getWorkFlowID());
				getContainerProperty(obj, WORKFLOW_VERSION).setValue(task.getWorkFlowVersion());
				getContainerProperty(obj, PROCESS_REPOSITORY_FIELD).setValue(task.getProcessCaseRepositoryField());
				getContainerProperty(obj, PROCESS_SCRIPT_NAME).setValue(task.getProcessTaskScriptName());
			}
			else
				getContainerProperty(obj, WORKFLOW_ID).setValue(new WorkFlowId(NOP));//Not a process

			getContainerProperty(obj, TYPE).setValue(task.getType());
		}
	}

	public void buttonClick(ClickEvent event)
	{
		Button clickedButton = event.getButton();

		if(m_skipButtonsMap.keySet().contains(clickedButton))
			for(ITaskPanelSkipListener l : m_skipListeners)
			{
				l.notifySkipButtonClicked(m_skipButtonsMap.get(clickedButton));
			}

		if(m_assignButtonsMap.keySet().contains(clickedButton))
			for(ITaskPanelAssignListener l : m_assignListeners)
			{
				l.notifyAssignButtonClicked(m_assignButtonsMap.get(clickedButton));
			}

		if(m_expireButtonsMap.keySet().contains(clickedButton))
			for(ITaskPanelExpireListener l : m_expireListeners)
			{
				l.notifyExpireButtonClicked(m_expireButtonsMap.get(clickedButton));
			}
	}

	public void addListener(ITaskPanelExpireListener listener)
	{
		m_expireListeners.add(listener);
	}

	public void addListener(ITaskPanelSkipListener listener)
	{
		m_skipListeners.add(listener);
	}

	public void addListener(ITaskPanelAssignListener listener)
	{
		m_assignListeners.add(listener);
	}
}
